package com.wuxinwudai.android.app;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import com.wuxinwudai.android.util.ToastUtils;

/**
 * TrafficStatsService 类为流量监控服务类
 * @author 吾心无待 于 2016年04月10日
 */
public class TrafficStatsService extends Service {
	private Handler handler = new Handler();
	private int counter;//计数器
	private int hour;
	private int minute;
	private int day;
	private int month;
	private int year;
	private String date;
	
	public final String DEV_FILE = "/proc/self/net/dev";//系统流量文件
	
	private String[] ethdata= {"0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"};
	private String[] gprsdata = {"0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"};
	private String[] wifidata = {"0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"};
	private String data="0,0,0,0,0,0,0,0,0,0,0,0";//对应 on.txt 里面的格式
	private String ETHLINE = "eth0";//eth 是以太网信息，wlan0 是 Wi-fi,rmnet0 是 GPRS
	private String GPRSLINE = "rmnet0";
	private String WIFILINE = "wlan0";
	private String TEXT_ENCODING = "UTF-8";
	private String ONPATH = "/data/data/zy.dnh/on.txt";
	private String LOGPATH = "/data/data/zy.dnh/log.txt";
	
	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}
	
	private Runnable tasks = new Runnable(){
		@Override
		public void run() {
			refresh();
			counter++;
			handler.postDelayed(tasks, 3000);//每 3 秒执行一次
		}		
	};
	
	//读取 dev 文件
	private void readDev(){
		FileReader fr = null;
		try{
			fr = new FileReader(DEV_FILE);
		}
		catch(FileNotFoundException ex){
			ToastUtils.showShort(this, "Could not read " + DEV_FILE);
		}
		BufferedReader in = new BufferedReader(fr,500);
		String line;
		String[] segs;
		String[] netdata;
		int count = 0;
		int k,j;
		try{
			while((line = in.readLine())!=null){
				segs = line.trim().split(":");
				if(line.startsWith(ETHLINE)){
					netdata = segs[1].trim().split(" ");
					for(k=0,j=0;k < netdata.length; k++){
						if(netdata[k].length() > 0){
							ethdata[j] = netdata[k];
							j++;
						}
					}
				}
				else if(line.startsWith(GPRSLINE)){
					netdata = segs[1].trim().split(" ");
					for(k = 0,j = 0; k < netdata.length; k++){
						if(netdata[k].length() > 0){
							gprsdata[j] = netdata[k];
							j++;
						}
					}
				}
				else if(line.startsWith(WIFILINE)){
					netdata = segs[1].trim().split(" ");
					for(k = 0,j = 0; k < netdata.length; k++){
						if(netdata[k].length() > 0){
							wifidata[j] = netdata[k];
							j++;
						}
					}
				}
				counter++;
			}
			fr.close();
		}
		catch(IOException ex){
			ToastUtils.showShort(this,ex.toString());
		}
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void onStart(Intent intent,int startId){
		handler.postDelayed(tasks,0);
		super.onStart(intent, startId);
	}
	
	@Override 
	public void onCreate(){
		super.onCreate();
	}
	
	@Override
	public void onDestroy(){
		handler.removeCallbacks(tasks);
		super.onDestroy();
	}
	
	
	public String getInfo(String path){
		File file;
		String str = "";
		FileInputStream in;
		try{
			file = new File(path);
			in = new FileInputStream(file);
			int length = (int) file.length();
			byte[] temp = new byte[length];
			in.read(temp,0,length);
			str = new String(temp,TEXT_ENCODING); 
			in.close();
		}catch(IOException ex){
			ToastUtils.showShort(this, ex.toString());
		}
		return str;
	}
	
	public void writeFile(String str,String path){
		File file;
		FileOutputStream out;
		try{
			file = new File(path);
			file.createNewFile();//创建文件
			out = new FileOutputStream(file);
			String infoToWrite = str;
			out.write(infoToWrite.getBytes());
			out.close();
		}catch(IOException ex){
			ToastUtils.showShort(this, ex.toString());
		}
	}

	public void refresh(){
		readDev();//读取本次开机之后知道当前系统的总流量
		data = ethdata[0] + "," + ethdata[1] + "," + ethdata[8] + "," + ethdata[9] + ","
				+ gprsdata[0] + "," + gprsdata[1] + "," + gprsdata[8] + "," + gprsdata[9] 
				+ wifidata[0] + "," + wifidata[1] + "," + wifidata[8] + "," + wifidata[9];
		String onstr = getInfo(ONPATH);//读取 on.txt 记录到 onstr 里
		String ondata[] = onstr.split(",");//将 onstr 各项分离放到 ondata 里
		//未完成
		
	}
}
